package com.example.personalizededucationsystem.repository;

import com.example.personalizededucationsystem.model.KnowledgePoint;
import com.example.personalizededucationsystem.utils.ApprovalStatus;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;
import java.util.Optional;

public interface KnowledgePointRepository extends JpaRepository<KnowledgePoint, Long> {

    Optional<KnowledgePoint> findByName(String name);

    List<KnowledgePoint> findAllByName(String name);

    Page<KnowledgePoint> findByStatus(ApprovalStatus status, Pageable pageable);

    Page<KnowledgePoint> findByStatusAndSubject(ApprovalStatus status, String subject, Pageable pageable);

    Page<KnowledgePoint> findByStatusAndNameContainingIgnoreCase(ApprovalStatus status, String name, Pageable pageable);

    @Query("SELECT k FROM KnowledgePoint k " +
            "WHERE k.status = :status AND k.subject = :subject AND " +
            "(LOWER(k.name) LIKE LOWER(CONCAT('%', :keyword, '%')) OR " +
            "(k.description IS NOT NULL AND LOWER(k.description) LIKE LOWER(CONCAT('%', :keyword, '%'))))")
    Page<KnowledgePoint> searchByStatusAndSubjectAndKeyword(
            @Param("status") ApprovalStatus status,
            @Param("subject") String subject,
            @Param("keyword") String keyword,
            Pageable pageable);
}